//
//  YSChangePwdViewController.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-9-24.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSChangePwdViewController.h"

@interface YSChangePwdViewController ()<UITextFieldDelegate> {
    
    __weak IBOutlet UITextField *oldPsdTextField;
    __weak IBOutlet UITextField *newPsdTextField;
    __weak IBOutlet UITextField *doubleNewPseTextField;
    __weak IBOutlet UIScrollView *contentView;
}

@end

@implementation YSChangePwdViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


- (IBAction)changePassword:(id)sender {
    if (oldPsdTextField.text.length == 0) {
        [self showMsgWithTitle:nil msg:@"旧设密码不能为空"];
        return;
    }
    
    if (newPsdTextField.text.length == 0) {
        [self showMsgWithTitle:nil msg:@"新设密码不能为空"];
        return;
    }
    
    if (doubleNewPseTextField.text.length == 0) {
        [self showMsgWithTitle:nil msg:@"确认密码不能为空"];
        return;
    }
    
    if (![newPsdTextField.text isEqualToString:doubleNewPseTextField.text]) {
        [self showMsgWithTitle:nil msg:@"新设密码与确认密码不一致"];
        return;
    }
    [YSProgressHUD showHUDAddedTo:self.view];
    [[NetworkMgr sharedManager] asynchRequestWithFunId:eAPI_CHANGE_PSD
                                              delegate:self
                                                params:@{@"oldPassword": oldPsdTextField.text,
                                                         @"newPassword": newPsdTextField.text}];
}

#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if (textField == oldPsdTextField) {
        [contentView setContentOffset:CGPointMake(0, 10) animated:YES];
    } else if (textField == newPsdTextField) {
        [contentView setContentOffset:CGPointMake(0, 36) animated:YES];
    } else {
        [contentView setContentOffset:CGPointMake(0, 56) animated:YES];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [contentView setContentOffset:CGPointMake(0, 0) animated:YES];
    [textField resignFirstResponder];
    return YES;
}

#pragma mark NetworkDelegate
- (void)didSuccess:(NetworkMgr *)api userInfo:(NSDictionary *)info {
    [YSProgressHUD hideHUDForView:self.view];
    
    NSInteger funId = [info[kFUN_ID] integerValue];
    
    if ([info[@"status"] intValue] != 200) {
        [self showToastMessage:info[@"msg"]];
        return;
    }
    
    if (funId == eAPI_CHANGE_PSD) {
        [self showToastMessage:@"修改密码成功"];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
@end
